



class baseService2{

    constructor(model,expClass){
        this.model = model
        this.expClass = expClass
    }

    // 聚合查询
    async aggregate(grass){
        let  result
        try {
            result = await  this.model.aggregate(grass)
        } catch (error) {
            throw new this.expClass(error)
        }
        return result
    }

    // 新增
    async create(data){
        let result
            try {
                result = await  this.model(data).save()
            } catch (error) {
                throw new this.expClass(error)
            }
            return result
        }

        // 根据ID删除1个
    async findByIdAndDelete(id){
            let result
                try {
                    result = await this.model.findByIdAndDelete(id)
                } catch (error) {
                    throw new this.expClass(error)
                }
                return result
            }

        // 根据条件查找1个并删除
        async findOneAndDelete(conditions){
            let result
                try {
                    result = await this.model.findOneAndDelete(conditions)
                } catch (error) {
                    throw new this.expClass(error)
                }
                return result
            }

         

            // 根据id查找并更新
        async findByIdAndUpdate(id,updates){
            let result
                try {
                    result = await  this.model.findByIdAndUpdate(id,updates)
                } catch (error) {
                    throw new this.expClass(error)
                }
                return result
            }

       async findOneAndUpdate(conditions,updates,options){
                let result
                    try {
                        result = await  this.model.findOneAndUpdate(conditions,updates,options)
                    } catch (error) {
                        console.log(error);
                        throw new this.expClass(error)
                    }
                    return result
                }

        async updateOne(conditions,docs,options){
            let result
            try {
                result = await  this.model.updateOne(conditions,docs,options)
            } catch (error) {
                throw new this.expClass(error)
            }
            return result
        }        



            // 查找单个
    async findOne(conditions,hides,fills,skip,limit,options){
        let  result
  
        if(fills==undefined||Object.keys(fills).length==0){
            try {
                result = await  this.model.findOne(conditions,hides,options).skip(skip).limit(limit)
            } catch (error) {
                throw new this.expClass(error)
            }
            return result
        }else {
            switch (Object.keys(fills).length) {
                case 1:
                    try {
                        result = await  this.model.findOne(conditions,hides,options).populate(Object.values(fills)[0]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                    break;
                case 2:
                    try {
                        result = await  this.model.findOne(conditions,hides,options).populate(Object.values(fills)[0]).populate(Object.values(fills)[1]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                  break;
                  case 3:
                    try {
                 
                        result = await  this.model.findOne(conditions,hides,options).populate(Object.values(fills)[0]).populate(Object.values(fills)[1]).populate(Object.values(fills)[2]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                  break;
                  case 4:
                    try {
                        result  = await  this.model.findOne(conditions,hides,options).populate(Object.values(fills)[0]).populate(Object.values(fills)[1]).populate(Object.values(fills)[2]).populate(Object.values(fills)[3]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                  break;
              }
        }
        }

        // 根据条件查找,包含多个
    async find(conditions,hides,fills,skip,limit){
        let  result

   
        if(fills==undefined||Object.keys(fills).length==0){

            try {
                result = await  this.model.find(conditions,hides).skip(skip).limit(limit)
            } catch (error) {
                throw new this.expClass(error)
            }
            return result
        }else {
            switch (Object.keys(fills).length) {
                case 1:
                    try {
                        result = await  this.model.find(conditions,hides).populate(Object.values(fills)[0]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                    break;
                case 2:
                    try {
                 
                        result = await  this.model.find(conditions,hides).populate(Object.values(fills)[0]).populate(Object.values(fills)[1]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                  break;
                  case 3:
                    try {
                 
                        result = await  this.model.find(conditions,hides).populate(Object.values(fills)[0]).populate(Object.values(fills)[1]).populate(Object.values(fills)[2]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                  break;
                  case 4:
                    try {
                        result = await  this.model.find(conditions,hides).populate(Object.values(fills)[0]).populate(Object.values(fills)[1]).populate(Object.values(fills)[2]).populate(Object.values(fills)[3]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                  break;
                  case 5:
                    try {
                        result = await  this.model.find(conditions,hides).populate(Object.values(fills)[0]).populate(Object.values(fills)[1]).populate(Object.values(fills)[2]).populate(Object.values(fills)[3]).populate(Object.values(fills)[4]).skip(skip).limit(limit)
                    } catch (error) {
                        throw new this.expClass(error)
                    }
                    return result
                    break;

             
     
    
              }

        }
        
   

        }

    }

  













    


module.exports={baseService2}